/**************************************************************************************
 *
 * @(#) GlobalExceptionHandler.java
 * @Package com.jgdt.jgdtkasannong_university_gene_backend.config
 **************************************************************************************/
package com.jgdt.jgdtkasannong_university_gene_backend.common.example.global;

import com.jgdt.jgdtkasannong_university_gene_backend.common.enums.SystemCodeEnums;
import com.jgdt.jgdtkasannong_university_gene_backend.common.example.custom.AuthenticationException;
import com.jgdt.jgdtkasannong_university_gene_backend.common.example.custom.BusinessException;
import com.jgdt.jgdtkasannong_university_gene_backend.common.response.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: 全局异常处理类,全局捕获异常自定义返回响应
 * @author: huangAn
 * @createDate: 2025/04/18
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     *  处理自定义业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultVo<Void> handleBusinessException(BusinessException e) {
        return ResultVo.failCustomize(String.valueOf(e.getCode()), e.getMessage());
    }

    /**
     * 系统级异常（如空指针、类型转换错误）
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultVo handleSystemException(Exception e) {
        log.error("系统异常err:",e);
        return ResultVo.failCustomize(SystemCodeEnums.FAIL.getKey(), e.getMessage());
    }

    /**
     * 入参异常
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultVo handleValidationException(MethodArgumentNotValidException e) {
        return ResultVo.failCustomize(SystemCodeEnums.BAD_REQUEST.getKey(), e.getMessage());
    }

    /**
     * 认证异常
     * @param e
     * @return
     */
    @ExceptionHandler(AuthenticationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultVo handleAuthenticationException(AuthenticationException e) {
        return ResultVo.failCustomize(SystemCodeEnums.BAD_REQUEST.getKey(), e.getMessage());
    }

}





